import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.InputStream;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class DesignPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = -3206186187070289393L;
	String chosenDesign = "State Farm Theme 1";
	ImageIcon pc = new ImageIcon(getClass().getClassLoader().getResource(
			"Images/stateFarmTheme1Icon.png"));
	JLabel previewLabel = new JLabel(pc, SwingConstants.CENTER);
	JLabel statusLabel = new JLabel(" ");
	final Color designColor = Color.WHITE;

	public DesignPanel() {
		super();
		this.setBackground(designColor);

		// set overall layout to border layout
		this.setLayout(new BorderLayout(50, 50));

		// Create line start panel

		// create buttons and add action listeners

		JRadioButton stateFarmTheme1Button = createRadioButton(
				"State Farm Theme 1 Select", KeyEvent.VK_1, true);
		stateFarmTheme1Button.setBackground(Color.WHITE);
		JRadioButton christmasButton = createRadioButton(
				"Christmas Theme Select", KeyEvent.VK_2, false);
		christmasButton.setBackground(Color.WHITE);
		JRadioButton customTheme1Button = createRadioButton(
				"Custom Theme 1 Select", KeyEvent.VK_3, false);
		customTheme1Button.setBackground(Color.WHITE);
		JRadioButton customTheme2Button = createRadioButton(
				"Custom Theme 2 Select", KeyEvent.VK_4, false);
		customTheme2Button.setBackground(Color.WHITE);

		JLabel stateFarmTheme1Label = new JLabel("State Farm Theme 1");
		JLabel christmasLabel = new JLabel("Christmas Theme");
		JLabel customTheme1Label = new JLabel("Custom Theme 1");
		JLabel customTheme2Label = new JLabel("Custom Theme 2");

		JButton edit1 = new JButton("Edit");
		JButton edit2 = new JButton("Edit");
		JButton edit3 = new JButton("Edit");
		JButton edit4 = new JButton("Edit");

		JButton updateTheme = new JButton("Update Theme");

		// Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(stateFarmTheme1Button);
		group.add(christmasButton);
		group.add(customTheme1Button);
		group.add(customTheme2Button);

		// Register a listener for the radio buttons.
		stateFarmTheme1Button.addActionListener(this);
		christmasButton.addActionListener(this);
		customTheme1Button.addActionListener(this);
		customTheme2Button.addActionListener(this);
		updateTheme.addActionListener(this);

		// add components to panel
		JPanel LineStartPanel = new JPanel();
		LineStartPanel.setBackground(designColor);
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		GridBagLayout gridBag = new GridBagLayout();
		LineStartPanel.setLayout(gridBag);

		// first row
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.insets.set(10, 10, 10, 10);
		LineStartPanel.add(stateFarmTheme1Button, gridBagConstraints);

		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		LineStartPanel.add(stateFarmTheme1Label, gridBagConstraints);

		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 0;
		LineStartPanel.add(edit1, gridBagConstraints);

		// second row
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		LineStartPanel.add(christmasButton, gridBagConstraints);

		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		LineStartPanel.add(christmasLabel, gridBagConstraints);

		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 1;
		LineStartPanel.add(edit2, gridBagConstraints);

		// third row
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		LineStartPanel.add(customTheme1Button, gridBagConstraints);

		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		LineStartPanel.add(customTheme1Label, gridBagConstraints);

		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 2;
		LineStartPanel.add(edit3, gridBagConstraints);

		// fourth row
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		LineStartPanel.add(customTheme2Button, gridBagConstraints);

		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		LineStartPanel.add(customTheme2Label, gridBagConstraints);

		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 3;
		LineStartPanel.add(edit4, gridBagConstraints);

		// fifth row
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 4;
		LineStartPanel.add(updateTheme, gridBagConstraints);

		// sixth row
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 5;
		LineStartPanel.add(statusLabel, gridBagConstraints);

		// picture preview
		GridBagConstraints pictureGridBagConstraint = new GridBagConstraints();
		pictureGridBagConstraint.gridx = 3;
		pictureGridBagConstraint.gridy = 0;
		pictureGridBagConstraint.gridheight = 4;

		LineStartPanel.add(previewLabel, pictureGridBagConstraint);
		/* End Line Start Panel */

		// create center Panel
		JPanel centerPanel = new JPanel();
		GridBagLayout grid2 = new GridBagLayout();
		centerPanel.setLayout(grid2);
		centerPanel.setBackground(designColor);

		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.insets.set(10, 10, 10, 10);
		// first row
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.gridy = 0;

		centerPanel.add(new JButton("Upload Profile Picture"),
				gridBagConstraints2);

		// second row
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.gridy = 1;
		centerPanel.add(new JButton("Upload Header"), gridBagConstraints2);

		// third row
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.gridy = 2;
		centerPanel.add(new JButton("Upload Background Image"),
				gridBagConstraints2);

		// add panels to the border layout
		this.add(LineStartPanel, BorderLayout.LINE_START);
		this.add(centerPanel, BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String buttonClicked = e.getActionCommand();
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true)
				.setOAuthConsumerKey("tCol5Fg2bOKDQ4qwMhcTw")
				.setOAuthConsumerSecret(
						"pJeJmEoMS6pmeeZ3FYpQDtnf65Qs93CaZwywIMt6Eo")
				.setOAuthAccessToken(
						"1539800966-mZgBtWeRafw7oYE5PmYJ5JPa6U83wwn7IJup0jy")
				.setOAuthAccessTokenSecret(
						"2gOZyZdvSG4YWXC60OWCsQgRZaNuFEer1P4Df41U");
		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitter = tf.getInstance();
		if (buttonClicked.equals("Update Theme")) {
			if (chosenDesign.equals("State Farm Theme 1")) {
				InputStream testBackgroundImage = this.getClass()
						.getClassLoader()
						.getResourceAsStream("woodBackground.jpg");
				InputStream testHeaderFile = this.getClass().getClassLoader()
						.getResourceAsStream("redHeader.jpg");

				/*
				 * testBackgroundImage = new File(ClassLoader
				 * .getSystemResource("Images/woodBackground.jpg") .toURI());
				 * File testHeaderFile = new File(ClassLoader
				 * .getSystemResource("Images/redHeader.jpg").toURI());
				 */
				Theme theme1 = new Theme("000", "fff", "71066d", null, null,
						testBackgroundImage, false, testHeaderFile, false);
				boolean successful = false;
				int tries = 0;
				while (!successful && tries < 5) {
					try {
						TwitterDesignUpdate.changeTwitterTheme(twitter, theme1);
						statusLabel.setText("Design Successfully Changed");
						successful = true;
					} catch (TwitterException e1) {
						System.out.println("tries" + tries);
						tries++;
					}
				}

			}

			else if (chosenDesign.equals("Christmas Theme")) {

				/* Testing updating with resource stream */
				InputStream testBackgroundImage = this.getClass()
						.getClassLoader()
						.getResourceAsStream("christmasBackground2.jpg");
				InputStream testHeaderFile = this.getClass().getClassLoader()
						.getResourceAsStream("christmasHeader.jpg");

				/* End Test */
				// delete me File testBackgroundImage = new File(ClassLoader
				// .getSystemResource(
				// "Images/christmasBackground2.jpg").toURI());
				// testHeaderFile = new File(ClassLoader.getSystemResource(
				// "Images/christmasHeader.jpg").toURI());
				Theme theme1 = new Theme("000", "fff", "005812", null, null,
						testBackgroundImage, false, testHeaderFile, false);
				boolean successful = false;
				int tries = 0;
				while (!successful && tries < 5) {
					try {
						TwitterDesignUpdate.changeTwitterTheme(twitter, theme1);
						statusLabel.setText("Design Successfully Changed");
						successful = true;
					} catch (TwitterException e1) {
						System.out.println("tries" + tries);
						tries++;
					}
				}

			}
		} else if (buttonClicked.equals("State Farm Theme 1 Select")) {
			ImageIcon pc = new ImageIcon(getClass().getClassLoader()
					.getResource("Images/stateFarmTheme1Icon.png"));
			previewLabel.setIcon(pc);
			chosenDesign = "State Farm Theme 1";
			statusLabel.setText(" ");
		} else if (buttonClicked.equals("Christmas Theme Select")) {
			ImageIcon pc = new ImageIcon(getClass().getClassLoader()
					.getResource("Images/christmasIcon.png"));
			previewLabel.setIcon(pc);
			chosenDesign = "Christmas Theme";
			statusLabel.setText(" ");
		} else if (buttonClicked.equals("Custom Theme 1")) {
			// not implemented
		} else if (buttonClicked.equals("Custom Theme 2")) {
			// not implemented
		}

	}

	private JRadioButton createRadioButton(String buttonTitle, int keyEvent,
			boolean selected) {
		JRadioButton jRadioButton = new JRadioButton();
		jRadioButton.setMnemonic(keyEvent);
		jRadioButton.setActionCommand(buttonTitle);
		jRadioButton.setSelected(selected);
		return jRadioButton;
	}
}
